@echo off
chcp 65001 >nul 2>&1

:: 检查管理员权限
net session >nul 2>&1 || (
    echo 请你右键以管理员方式运行本脚本.
    pause
    exit /B
)

setlocal



:: 定义在线脚本地址
set "SCRIPT_URL=https://gitee.com/tantil/my_zerotier/raw/master/zerotier_shell/win/our_zerotier_install.bat"
set "CURRENT_SCRIPT_PATH=%~f0"

:: 下载更新脚本
echo Downloading update...
powershell -Command "[Net.ServicePointManager]::SecurityProtocol = 'Tls12'; $wc=New-Object Net.WebClient; try{$wc.DownloadFile('%SCRIPT_URL%','%TEMP%\our_zerotier_install.bat')}catch{exit 1}"
if %errorlevel% neq 0 (
    echo 下载更新脚本失败. 请检查网络连接.
    pause
) else (
    echo 下载更新脚本成功.
    echo 开始对比脚本.
)
:: 比较并更新脚本
fc "%CURRENT_SCRIPT_PATH%" "%TEMP%\our_zerotier_install.bat" >nul
if %errorlevel% neq 0 (
    echo 新版本被探测到. 更新中...
    echo 按任意键继续更新脚本.
    pause >nul
    copy /y "%CURRENT_SCRIPT_PATH%" "%CURRENT_SCRIPT_PATH%.bak" >nul && echo Backup created: %CURRENT_SCRIPT_PATH%.bak || (echo 已经创建原脚本备份.&pause)
    move /y "%TEMP%\our_zerotier_install.bat" "%CURRENT_SCRIPT_PATH%" >nul && echo 脚本更新成功. 请关闭本脚本后重新运行. || (echo 更新脚本失败.&pause)
    echo 更新失败按空格键将使用此老版本，按ctrl+c退出脚本.

) else (
    del "%TEMP%\our_zerotier_install.bat"
    echo 没有更新被找到或当前版本已经是最新版本.
)
pause

:: 检查 ZeroTier 安装
sc query "ZeroTierOneService" >nul 2>&1
if %errorlevel% neq 0 (
    echo ZeroTier One is not installed. Installing...
    powershell -Command "[Net.ServicePointManager]::SecurityProtocol = 'Tls12'; $wc=New-Object Net.WebClient; $wc.DownloadFile('https://download.zerotier.com/dist/ZeroTier%%20One.msi','%TEMP%\ZeroTierOne.msi')"
    if exist "%TEMP%\ZeroTierOne.msi" (
        msiexec /i "%TEMP%\ZeroTierOne.msi" /quiet /norestart /log "%TEMP%\ZeroTierInstall.log"
        if %errorlevel% neq 0 (
            echo 安装失败！查看日志: %TEMP%\ZeroTierInstall.log
            pause
            exit /B 1
        )
        del "%TEMP%\ZeroTierOne.msi"
        echo ZeroTier One installed successfully.
        timeout /t 5 >nul
    ) else (
        echo 下载失败，检查网络连接或URL.或手动安装
        pause
        exit /B 1
    )
) else (
    echo ZeroTier One is already installed.
)

:: 定义目录路径变量
set "ZT_PATH=C:\ProgramData\ZeroTier\One"
set "PLANET_URL=http://47.109.86.205:3000/planet?key=20bd079f175e7b40"
set "MOON_URL=http://47.109.86.205:3000/00000050c81dd3be.moon?key=20bd079f175e7b40"

REM 设置临时目录
set "TEMP_DIR=%TEMP%\ZeroTierTemp"
if not exist "!TEMP_DIR!" (
    mkdir "!TEMP_DIR!"
    echo Temporary directory !TEMP_DIR! created.
)

REM 检查目标目录是否存在，不存在则创建
if not exist "!ZT_PATH!\moon.d" (
    mkdir "!ZT_PATH!\moon.d"
    echo Directory !ZT_PATH!\moon.d created.
)

:: 下载文件到临时目录
echo downloading planet file...
powershell -Command "Invoke-WebRequest -Uri '%PLANET_URL%' -OutFile '%TEMP_DIR%\planet'" || (
    echo Planet file download failed
    set ERROR_FLAG=1
    goto END
)

echo downloading moon file...
powershell -Command "Invoke-WebRequest -Uri '%MOON_URL%' -OutFile '%TEMP_DIR%\00000050c81dd3be.moon'" || (
    echo Moon file download failed
    set ERROR_FLAG=1
    goto END
)

:: 移动 moon 文件
move /Y "%TEMP_DIR%\00000050c81dd3be.moon" "%ZT_PATH%\moon.d\" 1>nul || (
    echo moon file move failed
    set ERROR_FLAG=1
    goto END
)

:: 替换 planet 文件
move /Y "%TEMP_DIR%\planet" "%ZT_PATH%\planet" 1>nul || (
    echo planet file replace failed
    set ERROR_FLAG=1
    goto END
)

echo Success set planet and moon!

:END
rmdir /S /Q "%TEMP_DIR%"
pause




:: 重启服务
:: 尝试正常停止ZeroTier One服务
echo 正在尝试停止ZeroTier服务...
net stop "ZeroTierOneService" >nul 2>&1 && (
    echo ZeroTier服务已成功停止。
) || (
    echo 无法正常停止ZeroTier服务，正在尝试强制关闭相关进程...

    :: 强制终止所有与ZeroTier相关的进程
    taskkill /F /IM zerotier_desktop_ui.exe /T >nul 2>&1
    :: 再次检查ZeroTier服务状态以确认是否已经停止
    sc query "ZeroTierOneService" | findstr /C:"STOPPED" >nul
    if %errorlevel% == 0 (
        echo ZeroTier相关进程已被强制关闭。
        timeout /t 5 >nul
        echo 正在尝试停止ZeroTier服务...
        net stop "ZeroTierOneService"
        timeout /t 5 >nul
        net stop "ZeroTierOneService" >nul 2>&1 && (
        echo ZeroTier服务已成功停止。
        )
    ) else (
        echo 强制关闭ZeroTier相关进程失败，请手动检查。
        pause
        exit /B
    )
)

:: 启动ZeroTier One服务
echo 正在尝试启动ZeroTier服务...
net start "ZeroTierOneService" >nul 2>&1 && (
    echo ZeroTier服务已成功启动。
) || (
    echo 无法启动ZeroTier服务，我也不知道为什么.
    pause
    exit /B
)

timeout /t 5 >nul
pause

:: 定义ZeroTier CLI路径
set "ZT_CLI=C:\ProgramData\ZeroTier\One\zerotier-one_x64.exe"
set "NETWORK_ID=50c81dd3be080b82"
:: 检查ZeroTier CLI是否存在
if exist "%ZT_CLI%" (
    :: 使用管理员权限执行join命令
    powershell -Command "Start-Process -Wait -Verb RunAs '%ZT_CLI%' -ArgumentList '-q', 'join', '%NETWORK_ID%'"

    :: 获取并显示ZeroTier状态信息
    "%ZT_CLI%" -q info || (
        echo Failed to execute ZeroTier CLI command.
        pause
        exit /B
    )
) else (
    echo ZeroTier CLI not found.
    pause
    exit /B
)

echo 请出示你的200 info xxxxx 1.14.2 ONLINE 给管理员,如果显示有误请联系管理员.
pause

:: 定义要更新的IP地址和域名列表
set "IP_ADDRESS=10.145.238.142"
set "DOMAINS=xgreport.swjtuhc.top xgsys.swjtuhc.top nextcloud.swjtuhc.top paperdec.swjtuhc.top acgpt.swjtuhc.top maxkb.swjtuhc.top"

:: 创建临时hosts文件路径（在脚本当前目录下）
set "TEMP_HOSTS=%~dp0hosts.tmp"

:: 检查hosts文件是否存在
if not exist "C:\Windows\System32\drivers\etc\hosts" (
    echo C:\Windows\System32\drivers\etc\hosts file not found.
    pause
    exit /B
)

:: 备份原始hosts文件
copy "C:\Windows\System32\drivers\etc\hosts" "%TEMP_HOSTS%.bak"
if %errorlevel% neq 0 (
    echo Failed to backup the hosts file.
    pause
    exit /B
)

:: 清空临时hosts文件
type nul > "%TEMP_HOSTS%"

:: 读取原始hosts文件并写入临时hosts文件，同时添加新的解析记录
for /f "delims=" %%i in ('findstr /v /r /c:"^[ \t]*#.*$" "C:\Windows\System32\drivers\etc\hosts" ^| findstr /v /r /c:"^[ \t]*$"') do (
    set "line=%%i"
    setlocal enabledelayedexpansion
    set "skip="
    for %%d in (%DOMAINS%) do (
        if "!line!"=="%IP_ADDRESS% %%d" (
            set "skip=true"
        )
    )
    if not defined skip (
        echo(!line! >> "%TEMP_HOSTS%"
    )
    endlocal
)

:: 添加新的解析记录到临时hosts文件
for %%d in (%DOMAINS%) do (
    echo %IP_ADDRESS% %%d >> "%TEMP_HOSTS%"
)

:: 将临时hosts文件复制到脚本当前目录下的hosts文件
copy "%TEMP_HOSTS%" "%~dp0hosts" >nul 2>&1

if %errorlevel% equ 0 (
    echo 已经更新你的host.
    echo 请移动脚本目录下的hosts文件到C:\Windows\System32\drivers\etc 并替换掉原有的hosts文件.
) else (
    echo 失败了，请手动往hosts文件中添加域名解析.
)
pause